package com.beetlestech.nfe.main.biz.com.impl;

import java.util.ArrayList;
import java.util.List;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.fw.core.util.StringUtil;
import com.beetlestech.nfe.main.biz.com.HbVcourList;
import com.beetlestech.nfe.main.biz.com.dto.VcourseinfolistInfoDTO;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.base.StatementDTO;
import com.beetlestech.nfe.main.db.base.StatementUtil;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.view.VcourseinfolistDTO;


public class HbVcourListImpl extends AbstractDBLogic implements HbVcourList, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	protected static final String SH_INFO = "用户信息表";
	
	@Override
	public VcourseinfolistInfoDTO[]  getCourseList(
			String courseName,
			Integer courseTypePId,
			Integer courseTypeCId,
			String courseStart,
			String courseEnd,
			Integer courseStateCId){
		List<VcourseinfolistInfoDTO> courseList = new ArrayList<VcourseinfolistInfoDTO>();
		List<VcourseinfolistDTO> courseListDB = new ArrayList<VcourseinfolistDTO>();
		VcourseinfolistDTO vcourseinfolistDTO =new VcourseinfolistDTO();
		StatementDTO statementDTO = new StatementDTO();
		if(!StringUtil.isEmpty(courseName)){
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSENAME, StatementUtil.KEYWORD.LIKE ,courseName);
		}
		if(courseTypePId!=0){
				StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSETYPEPID, StatementUtil.KEYWORD.EQUAL ,courseTypePId);
			
					
		}
		if(courseTypeCId!=0){
//			if(courseTypeCId<=16){
				StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSETYPECID, StatementUtil.KEYWORD.EQUAL ,courseTypeCId);
//			}
					
		}
		if(!StringUtil.isEmpty(courseStart) &&!StringUtil.isEmpty(courseEnd)){
			
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSESTART, StatementUtil.KEYWORD.GREATER_EQUAL_THAN ,courseStart);
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSEEND, StatementUtil.KEYWORD.LESS_EQUAL_THAN ,courseEnd);
		}else if((courseStart==null || courseStart.equals("")) &&(courseEnd!=null && !courseEnd.equals(""))){
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSEEND, StatementUtil.KEYWORD.LESS_EQUAL_THAN ,courseEnd);
		}else if((courseStart!=null && !courseStart.equals("")) &&(courseEnd==null || courseEnd.equals(""))){
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSESTART, StatementUtil.KEYWORD.GREATER_EQUAL_THAN ,courseStart);
		}
		if(courseStateCId!=-1){
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSETYPESTATEPID, StatementUtil.KEYWORD.EQUAL ,Integer.parseInt(CATAGORY_P_COURSE_STATE.OWNER.toString()));
			StatementUtil.andWhere(statementDTO, VcourseinfolistDTO.COLUMN_COURSETYPESTATECID, StatementUtil.KEYWORD.EQUAL ,courseStateCId);
		}
		vcourseinfolistDTO.set_dynamicWhereCondition(statementDTO.getSQLStatement());
		
		courseListDB=queryDAO.queryForObjectList(VcourseinfolistDTO.SQL_SEARCH, vcourseinfolistDTO);
		
		if(courseListDB==null || courseListDB.size()==0){
			createApplicationException("errMsg", "没找到要查询的信息");
		}else{
			for(VcourseinfolistDTO item:courseListDB){
				courseList.add(convertToVcourseListInfo(item));
			}
		}
		return courseList.toArray(new VcourseinfolistInfoDTO[courseList.size()]);
		
	}
	
	private VcourseinfolistInfoDTO convertToVcourseListInfo(VcourseinfolistDTO item){
		VcourseinfolistInfoDTO vcoursefolistInfoDTO = new VcourseinfolistInfoDTO();
		vcoursefolistInfoDTO.setcourseid(item.getcourseid());
		vcoursefolistInfoDTO.setcourseend(item.getcourseend());
		vcoursefolistInfoDTO.setcoursename(item.getcoursename());
		vcoursefolistInfoDTO.setcoursestart(item.getcoursestart());
		vcoursefolistInfoDTO.setcoursetypecname(item.getcoursetypecname());
		vcoursefolistInfoDTO.setcoursetypepname(item.getcoursetypepname());
		vcoursefolistInfoDTO.setcoursetypestate(item.getcoursetypestate());
		vcoursefolistInfoDTO.setdealnum(item.getdealnum());
		return vcoursefolistInfoDTO;
	}
}
